<#--
  Service 标准 CRUD 方法
  
  包含：
  - queryPage: 分页查询
  - getDetail: 查询详情
  - queryList: 列表查询
  - create: 创建
  - update: 更新
  - deleteById: 删除
  - deleteBatch: 批量删除
-->

    @Override
    public PageResponse<${table.entityName}DTO> queryPage(${table.entityName}DTO queryDTO) {
        // 构建查询条件
        LambdaQueryWrapper<${table.entityName}> wrapper = buildQueryWrapper(queryDTO);
        
        // 分页查询（PageQuery 已有默认值）
        long current = PageUtils.current(queryDTO);
        long size = PageUtils.size(queryDTO);
        Page<${table.entityName}> page = new Page<>(current, size);
        ${lowerEntity}Mapper.selectPage(page, wrapper);
        
        // 转换为DTO并返回
        return PageConverters.map(
            page.getRecords(), 
            page.getTotal(), 
            page.getCurrent(), 
            page.getSize(),
            entity -> EntityDTOConverter.toDTO(entity, ${table.entityName}DTO.class));
    }

    @Override
    public ${table.entityName}DTO getDetail(${pkType} id) {
        if (id == null) {
            throw BusinessException.invalidParam("ID不能为空");
        }
        
        ${table.entityName} entity = ${lowerEntity}Mapper.selectById(id);
        if (entity == null) {
            throw BusinessException.resourceNotFound("${table.comment!table.entityName}不存在");
        }
        
        ${table.entityName}DTO dto = EntityDTOConverter.toDTO(entity, ${table.entityName}DTO.class);
        
        return dto;
    }

    @Override
    public List<${table.entityName}DTO> queryList(${table.entityName}DTO queryDTO) {
        LambdaQueryWrapper<${table.entityName}> wrapper = buildQueryWrapper(queryDTO);
        List<${table.entityName}> entities = ${lowerEntity}Mapper.selectList(wrapper);
        
        return entities.stream()
            .map(entity -> EntityDTOConverter.toDTO(entity, ${table.entityName}DTO.class))
            .collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ${table.entityName}DTO create(${table.entityName}DTO dto) {
        // 转换为实体
        ${table.entityName} entity = EntityDTOConverter.toEntity(dto, ${table.entityName}.class);
        
        // 插入数据库
        ${lowerEntity}Mapper.insert(entity);
        
        // 返回DTO
        return EntityDTOConverter.toDTO(entity, ${table.entityName}DTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ${table.entityName}DTO update(${pkType} id, ${table.entityName}DTO dto) {
        if (id == null) {
            throw BusinessException.invalidParam("ID不能为空");
        }
        
        // 检查是否存在
        ${table.entityName} existing = ${lowerEntity}Mapper.selectById(id);
        if (existing == null) {
            throw BusinessException.resourceNotFound("${table.comment!table.entityName}不存在");
        }
        
        // 只更新非null字段（防止覆盖未修改的字段）
        EntityDTOConverter.updateEntity(dto, existing);
        
        // 更新数据库
        ${lowerEntity}Mapper.updateById(existing);
        
        // 返回DTO
        return EntityDTOConverter.toDTO(existing, ${table.entityName}DTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(${pkType} id) {
        if (id == null) {
            throw BusinessException.invalidParam("ID不能为空");
        }
        
        ${table.entityName} existing = ${lowerEntity}Mapper.selectById(id);
        if (existing == null) {
            throw BusinessException.resourceNotFound("${table.comment!table.entityName}不存在");
        }
        
        ${lowerEntity}Mapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<${pkType}> ids) {
        if (ids == null || ids.isEmpty()) {
            throw BusinessException.invalidParam("ID列表不能为空");
        }
        
        ${lowerEntity}Mapper.deleteBatchIds(ids);
    }

